using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace VTGGoldMiner
{
    //lop duoc nhap vao tu thong so da co dan ti le va xu ly theo co dan ti le.
    //dau vao la cac hinh anh da duoc bo ty le.
    class HinhAnh
    {
        public Rectangle bohinhanh;
        private Texture2D anh1, anh2;
        private string tenhinhanh;
        public int slchon = 0;
        public int nhapnhay = 0;
        private int chayamchon = 0, chayamchuyen = 0;//bang 0 thi khong chay am
        private SoundEffect amchon, amchuyen;
        private int luotkichchon;
        public HinhAnh(string ten, Rectangle boanh, Texture2D nhapanh1, Texture2D nhapanh2, SoundEffect am1, SoundEffect am2)
        {
            tenhinhanh = ten;
            bohinhanh = boanh;
            anh1 = nhapanh1;
            anh2 = nhapanh2;
            if (am1 == null) chayamchon = 0;
            else { chayamchon = 1; amchon = am1; }
            if (am2 == null) chayamchuyen = 0;
            else { chayamchuyen = 1; amchuyen = am2; }
        }
        public void DichChuyen(int tdx, int tdy, float tilerong, float tiledai)
        {
            bohinhanh.X = Convert.ToInt32(tdx * tilerong);
            bohinhanh.Y = Convert.ToInt32(tdy * tiledai);
        }
        public void KichChonThuong(MouseState chuothientai, MouseState chuottruocdo, ref string thongbao, float chinham)
        {
            if (chuothientai.LeftButton == ButtonState.Pressed && chuottruocdo.LeftButton == ButtonState.Released)
            {
                if (chuothientai.X > bohinhanh.X && chuothientai.X < bohinhanh.X + bohinhanh.Width && chuothientai.Y > bohinhanh.Y && chuothientai.Y < bohinhanh.Y + bohinhanh.Height)
                {
                    if (chayamchon == 1) amchon.Play(chinham, 0, 0);
                    thongbao = tenhinhanh;
                }
            }
        }
        public void KichChuyenDoi(MouseState chuothientai, MouseState chuottruocdo, ref string thongbao, float chinham)
        {
            if (chuothientai.LeftButton == ButtonState.Pressed && chuottruocdo.LeftButton == ButtonState.Released)
            {
                if (chuothientai.X > bohinhanh.X && chuothientai.X < bohinhanh.X + bohinhanh.Width && chuothientai.Y > bohinhanh.Y && chuothientai.Y < bohinhanh.Y + bohinhanh.Height)
                {
                    if (chayamchon == 1) amchon.Play(chinham, 0, 0);
                    Texture2D anhtg = anh1;
                    anh1 = anh2; anh2 = anhtg;
                    thongbao = tenhinhanh;
                }
            }
        }
        public void KichChonNhapNhay(MouseState chuothientai, MouseState chuottruocdo, int luotkich, ref string thongbao, float chinham)
        {
            if (slchon == luotkich)
            {
                nhapnhay++;
                if (nhapnhay == 30)
                {
                    if (chayamchuyen == 1) amchuyen.Play(chinham, 0, 0);
                    nhapnhay = 0;
                    slchon = 0;
                    thongbao = tenhinhanh;
                }
            }
            else if (chuothientai.LeftButton == ButtonState.Pressed && chuottruocdo.LeftButton == ButtonState.Released)
            {
                if (chuothientai.X > bohinhanh.X && chuothientai.X < bohinhanh.X + bohinhanh.Width && chuothientai.Y > bohinhanh.Y && chuothientai.Y < bohinhanh.Y + bohinhanh.Height)
                {
                    luotkichchon = luotkich;
                    slchon++;
                    if (chayamchon == 1) amchon.Play(chinham, 0, 0);
                }
                else slchon = 0;
            }
        }
        public void HienThiThuong(SpriteBatch nenve)
        {
            nenve.Draw(anh1, bohinhanh, Color.White);
        }
        public void HienThiNhapNhay(SpriteBatch nenve)
        {
            if (slchon == 0) nenve.Draw(anh1, bohinhanh, Color.White);
            else if (slchon == luotkichchon)
            {
                if (nhapnhay % 5 == 0)
                    nenve.Draw(anh2, bohinhanh, Color.White);
            }
            else nenve.Draw(anh2, bohinhanh, Color.White);
        }
    }
}